<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class BillHistory extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		$this->load->view('v_billhistory');
	}
	
	public function getbills()
	{
		$this->load->model('m_billhistory');
		
		$recordset = $this->m_billhistory->get_user_id($this->session->userdata('useremail'));
		$results = $recordset->result();
		$userid = $results[0]->userId;
		
		$recordset = $this->m_billhistory->getlinkedvendors($userid);
		$linkedvendors = $recordset->result();
		
		$jsonObject->sEcho = $_POST['sEcho'];
		$jsonObject->iTotalRecords = 0;
		$jsonObject->iTotalDisplayRecords = 0;
				
		$jsonObject->aaData = array();
		
		foreach($linkedvendors as $vendorlink)
		{
			$recordset = $this->m_billhistory->get_vendor_name($vendorlink->Vendor_ID);
			$results = $recordset->result();
			$vendorname = $results[0]->Vendor_Name;
			
			$recordset = $this->m_billhistory->get_vendor_source($vendorlink->Vendor_ID);
			$results = $recordset->result();
			$vendorsource = $results[0]->Vendor_Source . '/getBills';
			
			$postdata = http_build_query(
				array(
					'userid' => $vendorlink->AccountNumber,
					'vendorid' => $vendorname
				)
			);
			
			$json = NULL;

			if(function_exists("curl_init"))
			{
				$curl_handle = curl_init();
				curl_setopt ($curl_handle, CURLOPT_POST, TRUE);
				curl_setopt ($curl_handle, CURLOPT_POSTFIELDS, $postdata);
				curl_setopt ($curl_handle, CURLOPT_URL, $vendorsource);
				curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
				curl_setopt ($curl_handle, CURLOPT_CONNECTTIMEOUT, TRUE);
				$json = curl_exec($curl_handle);
				curl_close($curl_handle);
			}
			else
			{
				$opts = array('http' =>
					array(
						'method'  => 'POST',
						'header'  => 'Content-type: application/x-www-form-urlencoded',
						'content' => $postdata
					)
				);
				
				$context  = stream_context_create($opts);

				$json = file_get_contents($url, false, $context);
			}

			$vendorObject = json_decode($json);

			if($vendorObject->code == 0)
			{		
				$bills = Array();
				for($i = 0; $i < count($vendorObject->bills); $i++)
				{
					if($vendorObject->bills[$i]->State == "Paid")
					{
						$bill = array();

						array_push($bill, $vendorObject->bills[$i]->Vendor);
						array_push($bill, $vendorObject->bills[$i]->DueDate);
						array_push($bill, $vendorObject->bills[$i]->Amount);
						array_push($bill, $vendorObject->bills[$i]->Description);
						array_push($bill, $vendorObject->bills[$i]->State);
						
						array_push($bills, $bill);
					}
				}
				$jsonObject->iTotalRecords += count($vendorObject->bills);

				
				$jsonObject->aaData = array_merge($jsonObject->aaData, $bills);
			}
		}
		
		$jsonObject->aaData = array_splice($jsonObject->aaData, $_POST['iDisplayStart'], $_POST['iDisplayStart'] + $_POST['iDisplayLength']);
		$jsonObject->iTotalDisplayRecords = min(count($jsonObject->aaData), $_POST['iDisplayLength']);
		echo json_encode($jsonObject);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */